package com.xhs.zdl.demo.addressChoose;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.gson.Gson;
import com.xhs.zdl.demo.MyUtils;
import com.xhs.zdl.demo.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AreaSelectActivity extends AppCompatActivity implements AreaFragment.OnFragmentInteractionListener {

    private AreaFragment fragment;
    private Map<String, Object> mapData = new HashMap<>();
    private Gson gson;
    private ArrayList<String> data;
    private boolean isEnd = false;
    private Map<String, Map> map = new HashMap<>();
    private Map<String, AreaFragment> fragmentMap = new HashMap<>();
    private Map<String, String> address = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_area_select);

        try {
            initView();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void initView() throws JSONException {
        gson = new Gson();

        ArrayList<String> list = new ArrayList<>();
        list.add("东区");
        list.add("西区");
        list.add("北区");
        Map<String, List<String>> map1 = new HashMap<>();
        map1.put("一楼", list);
        map1.put("三楼", list);
        Map<String, List<String>> map2 = new HashMap<>();
        map2.put("一楼", list);
        map2.put("二楼", list);
        map2.put("三楼", list);
        mapData.put("鑫豪斯大楼1",map1);
        mapData.put("鑫豪斯大楼2",map2);

        data = new ArrayList<>();
        Set<Map.Entry<String, Object>> entries = mapData.entrySet();
        for (Map.Entry<String, Object> en : entries){
            data.add(en.getKey());
        }

        map.put("1", mapData);
        fragment = AreaFragment.newInstance(data, 1);
        fragmentMap.put("1", fragment);
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content, fragment).commit();
    }

    @Override
    public void onFragmentInteraction(String key, int level) {
        if (null == key) {
            return;
        }

        address.put("level"+level, key);
        int nowLevel = level + 1;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (isEnd && !fragmentMap.containsKey((level+1)+"")){
            Intent intent = new Intent();
            intent.putExtra("data", gson.toJson(address));
            setResult(RESULT_OK,intent);
            finish();
        }else if (map.get(level+"").get(key) instanceof Map){
            MyUtils.Log(key+","+level);
            transaction.hide(fragmentMap.get(level+""));

            Map<String, Object> tempMap = (Map<String, Object>) map.get(level+"").get(key);
            map.put(nowLevel+"", tempMap);
            ArrayList<String> tempData = new ArrayList<>();
            Set<Map.Entry<String, Object>> entries = tempMap.entrySet();
            for (Map.Entry<String, Object> en : entries){
                tempData.add(en.getKey());
            }
            fragment = AreaFragment.newInstance(tempData, nowLevel);
            fragmentMap.put(nowLevel+"", fragment);

            transaction.add(R.id.content, fragment).addToBackStack(null).commit();
        }else if (map.get(level+"").get(key) instanceof ArrayList){
            isEnd = true;
            transaction.hide(fragmentMap.get(level+""));

            ArrayList<String> tempData = (ArrayList<String>) map.get(level+"").get(key);
            fragment = AreaFragment.newInstance(tempData, nowLevel);
            fragmentMap.put(nowLevel+"", fragment);

            transaction.add(R.id.content, fragment).addToBackStack(null).commit();
        }
    }

    /**
     * 解析json数据
     * @param object
     * @return
     * @throws JSONException
     */
    private Map<String, Object> getSitesMap(JSONObject object) throws JSONException {
        Map<String, Object> mapData = new HashMap<>();

        Iterator<String> keys = object.keys();
        List<String> keyList = new ArrayList<>();
        while (keys.hasNext()){
            keyList.add(keys.next());
        }
        for (int i = 0; i < keyList.size(); i++) {
            String key = keyList.get(i);
            JSONArray jsonArray = object.getJSONArray(key);
            List<String> tempList1 = new ArrayList<>();
            List<Map<String, Object>> tempList2 = new ArrayList<>();
            for (int j = 0; j < jsonArray.length(); j++) {
                if (!(jsonArray.get(0) instanceof String)) {
                    Map<String, Object> tempMap = getSitesMap(jsonArray.getJSONObject(j));
                    if (mapData.containsKey(key)) {
                        Map<String, Object> oldMap = (Map<String, Object>) mapData.get(key);
                        oldMap.putAll(tempMap);
                    }else
                        mapData.put(key, tempMap);
                }else {
                    tempList1.add(jsonArray.getString(j));
                    mapData.put(key, tempList1);
                }
            }
        }

        return mapData;
    }
}
